﻿#region Imports
using System;
using System.Collections.ObjectModel;
using System.ComponentModel;
using CommonUtilities.Attributes;
using CommonUtilities.Extensions;
using CommonUtilities.Properties;
using CommonUtilities.Util;
#endregion

namespace EntertainmentManager.Models
{
    public class SeriesInfo : UIToggleNotifier
    {
        #region Declarations
        /// <summary>
        /// Backing field for Seasons
        /// </summary>
        private ObservableCollection<SeasonInfo> seasons;

        /// <summary>
        /// Backing field for FullPath
        /// </summary>
        private string fullPath;
        #endregion

        #region Properties
        /// <summary>
        /// Series title
        /// </summary>
        [DataImportMap("TV Series")]
        [Description("Series name")]
        [DisplayName("Title")]
        [Sortable]
        [SortIndex(0)]
        public string SeriesTitle { get; set; }

        /// <summary>
        /// Episode URL
        /// </summary>
        //[ColumnType(typeof(Hyperlink))] // RKD - new attrib
        [DataImportMap("IMDb_id")]
        [Description("IMDb url")]
        [DisplayName("URL")]
        [Sortable]
        [SortIndex(2)]
        public string ImdbUrl { get; set; }

        /// <summary>
        /// Full path of series
        /// </summary>
        [DataImportMap("Location")]
        [Description("Full path")]
        [DisplayName("Path")]
        [SortIndex(3)]
        public string FullPath
        {
            get { return fullPath.IsBlank() ? fullPath : fullPath.AppendPath(SeriesTitle); }
            private set { fullPath = value; }
        }

        /// <summary>
        /// Collection of seasons encapsulated by series
        /// </summary>
        [Browsable(false)]
        public ObservableCollection<SeasonInfo> Seasons
        {
            get { return seasons; }
            set
            {
                seasons = value;
                seasons._ForEach(ssn =>
                {
                    ssn.FullPath = FullPath.AppendPath(ssn.SeasonNameLong);
                    ssn.Episodes._ForEach(ep => ep.FullPath = ssn.FullPath.AppendPath(ep.FileNameFull));
                }); // Update full paths

                YearMinMaxPair = new MinMaxPair<int>(seasons._Select(ssn => ssn.YearMinMaxPair));
            }
        }

        /// <summary>
        /// Min-Max pair for series duration (in years)
        /// </summary>
        [Browsable(false)]
        internal MinMaxPair<int> YearMinMaxPair { get; set; }

        /// <summary>
        /// Duration of the series (as period in years)
        /// </summary>
        [Description("Duration")]
        [SortIndex(1)]
        public string Duration
        {
            get { return MinMaxPair<int>.GenerateDuration(YearMinMaxPair); }
        }
        #endregion

        #region Constructors
        /// <summary>
        /// Initializes a new default instance of <code>EntertainmentManager.Models.SeriesInfo</code>
        /// </summary>
        public SeriesInfo()
            : base()
        {
            FullPath = Resources.Const_NotAvailable;
        }

        /// <summary>
        /// Initializes a new instance of <code>EntertainmentManager.Models.SeriesInfo</code>
        /// </summary>
        /// <param name="basePath">Base path of the series</param>
        public SeriesInfo(string basePath)
            : this()
        {
            if (!basePath.IsBlank()) FullPath = basePath;
        }
        #endregion
    }

    [Serializable]
    public class SeriesInfoMirror
    {
        #region Properties
        public string SeriesTitle { get; set; }
        public string Duration { get; set; }
        public string ImdbUrl { get; set; }
        public string FullPath { get; set; }
        #endregion Properties
    }
}
